//
//  ZBModelPopview.m
//  YouHui
//
//  Created by  on 11-12-7.
//  Copyright (c) 2011年 netease. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ZBModelPopview.h"

@implementation ZBModelPopview

@synthesize contentView=_contentView;
@synthesize delegate=_delegate;
@synthesize borderColor=_borderColor;
@synthesize borderWidth=_borderWidth;

-(id)init {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    return [self initWithFrame:window.bounds];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor =[[UIColor darkGrayColor] colorWithAlphaComponent:0.5];
    }
    return self;
}

-(void)dealloc {
    [_borderColor release];
    [super dealloc];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
-(void)setContentView:(UIView *)contentView {
    [contentView retain];
    [_contentView removeFromSuperview];
    _contentView = contentView;
    [self addSubview:contentView];
    [contentView release];
    
    contentView.frame = CGRectCenterRect(self.bounds,contentView.frame.size);
}

-(void)show {
    UIWindow *window = [UIApplication sharedApplication].keyWindow; 
    [window addSubview:self];
    [window bringSubviewToFront:self];
    
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
	animation.duration = 0.3;
    NSValue *value0 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)];
    NSValue *value1 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)];
    NSValue *value2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)];
    NSValue *value3 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.values =[NSArray arrayWithObjects:value0,value1,value2,value3,nil];
    animation.keyTimes =[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],
                         [NSNumber numberWithFloat:0.6],
                         [NSNumber numberWithFloat:0.8],
                         [NSNumber numberWithFloat:1.0],nil];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
	[self.contentView.layer addAnimation:animation forKey:@"keyframeAnimation"];
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.fromValue = [NSNumber numberWithFloat:0.5];
    alphaAnimation.toValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.duration = 0.3;
	[self.contentView.layer addAnimation:alphaAnimation forKey:@"alphaAnimation"];     
}

-(void)dismissWithButtonIndex:(NSUInteger)index; {
    [UIView animateWithDuration:0.2 
                     animations:^{
                         self.alpha = 0.4;
                     } 
                     completion:^(BOOL finished) {
                         [self removeFromSuperview]; 
                         if (self.delegate) {
                             [self.delegate zbPopView:self dismissedWithButtonIndex:index];
                         }
                     }];
}

@end
